package com.mianyang.zhangsan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.entity.SecDept;
import com.mianyang.zhangsan.service.ISecDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 部门接口
 * @author jiangjr
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/dept")
public class SecDeptController extends BaseController {
    @Autowired
    private ISecDeptService deptService;

    /**
     * 查询所属部门
     */
    @GetMapping("/self/{userId}")
    public R searchSelfDept(@PathVariable("userId") Long userId) {
        SecDept dept = deptService.selectDept(userId);
        return success("dept", dept);
    }

    /**
     * 查询所有部门
     */
    @GetMapping("/all")
    public R searchAllDept(@RequestParam(value = "current", defaultValue = "1") Integer current,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SecDept> dept = deptService.page(new Page<>(current, pageSize));
        return success("dept", dept);
    }

    /**
     * 新增部门
     */
    @PostMapping("/save")
    public R saveDept(@RequestBody SecDept dept) {
        deptService.save(dept);
        return success();
    }

    /**
     * 修改部门
     */
    @PutMapping("/edit")
    public R editDept(@RequestBody SecDept dept) {
        deptService.updateById(dept);
        return success();
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/delete/{deptId}")
    public R deleteDept(@PathVariable("deptId") Long deptId) {
        deptService.removeById(deptId);
        return success();
    }
}
